package com.huika.renrench.control.common.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.huika.renrench.R;
import com.huika.renrench.support.bean.ContactBean;


/**
 * @description 详细描述：联系人的适配
 * @date 2014年6月11日 下午9:38:02
 */
public class ContactAdapter extends BaseAdapter {
	private List<ContactBean> listDatas;
	private Context mContext;
	private LayoutInflater inflater;
	private InviteOnclik mInviteOnclik;

	public ContactAdapter(List<ContactBean> list, Context context, InviteOnclik inviteOnclik) {
		super();
		this.listDatas = list;
		this.mContext = context;
		this.mInviteOnclik = inviteOnclik;
		inflater = LayoutInflater.from(mContext);
	}

	public void updateListView(List<ContactBean> list) {
		if (listDatas != null)
			listDatas.clear();
		listDatas.addAll(list);
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return listDatas.size();
	}

	@Override
	public Object getItem(int position) {
		return listDatas.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public int getSectionForPosition(int position) {
		return listDatas.get(position).getSortLetters().charAt(0);
	}

	public int getPositionForSection(int section) {
		for (int i = 0; i < getCount(); i++) {
			String sortStr = listDatas.get(i).getSortLetters();
			char firstChar = sortStr.toUpperCase().charAt(0);
			if (firstChar == section) {
				return i;
			}
		}
		return -1;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder;
		final ContactBean mContent = listDatas.get(position);
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.mobile_contact_list_item, null);
			viewHolder = new ViewHolder();
			viewHolder.select_contacts_rl = (RelativeLayout) convertView.findViewById(R.id.select_contacts_rl);
			viewHolder.mContactNameTv = (TextView) convertView.findViewById(R.id.mobile_contact_name_tv);
			viewHolder.mContactPhoneTv = (TextView) convertView.findViewById(R.id.mobile_contact_phone_tv);
			viewHolder.mCatalogTv = (TextView) convertView.findViewById(R.id.catalog_tv);
//			viewHolder.mGoToInvite = (TextView) convertView.findViewById(R.id.tv_invite_friend);
			convertView.setTag(viewHolder);
		}
		else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		int section = getSectionForPosition(position);
		if (position == getPositionForSection(section)) {
			viewHolder.mCatalogTv.setVisibility(View.VISIBLE);
			String tempSort = mContent.getSortLetters();
			viewHolder.mCatalogTv.setText(tempSort);
		}
		else {
			viewHolder.mCatalogTv.setVisibility(View.GONE);
		}
		viewHolder.select_contacts_rl.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mInviteOnclik.tvOnclick(mContent);
				
			}
		});
		viewHolder.mContactNameTv.setText(mContent.getDisplayName());
		viewHolder.mContactPhoneTv.setText(mContent.getPhoneNum());
		return convertView;
	}

	private class ViewHolder {
		private RelativeLayout select_contacts_rl;
		private TextView mContactNameTv;
		private TextView mContactPhoneTv;
		private TextView mCatalogTv;
//		private TextView mGoToInvite;
	}

	public interface InviteOnclik{
		void tvOnclick(ContactBean mContent);
	}
}
